package com.client.act;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.client.bean.BirdMessage;
import com.client.common.ValueStack;
import com.client.db.DataService;
import com.client.task.SynchroziedRunnable;
import com.client.task.TimeSynchroziedRunnable;
import com.client.task.UploadBirdClickNumMessagesTask;
import com.clinet.cache.AppConstants;

public class BaseAct extends Activity {
	/**
	 * 创建的时候进行内存回收
	 */
	@SuppressWarnings("static-access")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (this instanceof BirdSearchAct) {
			if (ValueStack.getInstance().activitys.containsKey("BirdSearchAct")) {
				ValueStack.getInstance().activitys.get("BirdSearchAct")
						.finish();
			}
			ValueStack.getInstance().activitys.put("BirdSearchAct", this);

		} else if (this instanceof CollectionAct) {
			if (ValueStack.getInstance().activitys.containsKey("BirdSearchAct")) {
				ValueStack.getInstance().activitys.get("BirdSearchAct")
						.finish();
			}
			ValueStack.getInstance().activitys.put("CollectionAct", this);
		} else if (this instanceof CollectionGridAct) {
			if (ValueStack.getInstance().activitys
					.containsKey("CollectionGridAct")) {
				ValueStack.getInstance().activitys.get("CollectionGridAct")
						.finish();
			}
			ValueStack.getInstance().activitys.put("CollectionGridAct", this);
		}

		else if (this instanceof DetaiAct) {
			if (ValueStack.getInstance().activitys.containsKey("DetaiAct")) {
				ValueStack.getInstance().activitys.get("DetaiAct").finish();
			}
			ValueStack.getInstance().activitys.put("DetaiAct", this);
		}

		else if (this instanceof EidtPicAct) {
			if (ValueStack.getInstance().activitys.containsKey("EidtPicAct")) {
				ValueStack.getInstance().activitys.get("EidtPicAct").finish();
			}
			ValueStack.getInstance().activitys.put("EidtPicAct", this);
		}

		else if (this instanceof MainAct) {
			if (ValueStack.getInstance().activitys.containsKey("MainAct")) {
				ValueStack.getInstance().activitys.get("MainAct").finish();
			}
			ValueStack.getInstance().activitys.put("MainAct", this);
		}

		else if (this instanceof NameSearchAct) {
			if (ValueStack.getInstance().activitys.containsKey("NameSearchAct")) {
				ValueStack.getInstance().activitys.get("NameSearchAct")
						.finish();
			}
			ValueStack.getInstance().activitys.put("NameSearchAct", this);
		}

		else if (this instanceof PropertyAct) {
			if (ValueStack.getInstance().activitys.containsKey("PropertyAct")) {
				ValueStack.getInstance().activitys.get("PropertyAct").finish();
			}
			ValueStack.getInstance().activitys.put("PropertyAct", this);
		}

		else if (this instanceof SartAct) {
			if (ValueStack.getInstance().activitys.containsKey("SartAct")) {
				ValueStack.getInstance().activitys.get("SartAct").finish();
			}
			ValueStack.getInstance().activitys.put("SartAct", this);
		}

		else if (this instanceof SettingAct) {
			if (ValueStack.getInstance().activitys.containsKey("SettingAct")) {
				ValueStack.getInstance().activitys.get("SettingAct").finish();
			}
			ValueStack.getInstance().activitys.put("SettingAct", this);
		}

		else if (this instanceof TypeAct) {
			if (ValueStack.getInstance().activitys.containsKey("TypeAct")) {
				ValueStack.getInstance().activitys.get("TypeAct").finish();
			}
			ValueStack.getInstance().activitys.put("TypeAct", this);
		}

		else if (this instanceof UnunloadAct) {
			if (ValueStack.getInstance().activitys.containsKey("UnunloadAct")) {
				ValueStack.getInstance().activitys.get("UnunloadAct").finish();
			}
			ValueStack.getInstance().activitys.put("UnunloadAct", this);
		}

		else if (this instanceof UnunloadAnimalAct) {
			if (ValueStack.getInstance().activitys
					.containsKey("UnunloadAnimalAct")) {
				ValueStack.getInstance().activitys.get("UnunloadAnimalAct")
						.finish();
			}
			ValueStack.getInstance().activitys.put("UnunloadAnimalAct", this);
		}

		else if (this instanceof UnunloadBirdAct) {
			if (ValueStack.getInstance().activitys
					.containsKey("UnunloadBirdAct")) {
				ValueStack.getInstance().activitys.get("UnunloadBirdAct")
						.finish();
			}
			ValueStack.getInstance().activitys.put("UnunloadBirdAct", this);
		}

		else if (this instanceof UserLoginAct) {
			if (ValueStack.getInstance().activitys.containsKey("UserLoginAct")) {
				ValueStack.getInstance().activitys.get("UserLoginAct").finish();
			}
			ValueStack.getInstance().activitys.put("UserLoginAct", this);
		} else if (this instanceof SettingAct) {
			if (ValueStack.getInstance().activitys.containsKey("SettingAct")) {
				ValueStack.getInstance().activitys.get("SettingAct").finish();
			}
			ValueStack.getInstance().activitys.put("SettingAct", this);
		}
		// 登录与动画
		if (this instanceof UserLoginAct || this instanceof SartAct) {
		} else {
			
			//打开定时查询线程
			AppConstants.execute(new TimeSynchroziedRunnable(this));
			if(mDataService==null)
			{	mDataService = new DataService(BaseAct.this);
				
			}
			if (!mDataService.hasBirdList()) {
				
			}else
			{
				//打开时时同步线程
				AppConstants.execute(new SynchroziedRunnable(this));	
			}
		
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean flag = false;
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			finish();
			flag = true;
			break;
		default:
			flag = super.onKeyDown(keyCode, event);
			break;
		}
		return flag;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (this instanceof CollectionAct) {
			if (ValueStack.getInstance().activitys.containsKey("BirdSearchAct")) {
				ValueStack.getInstance().activitys.get("BirdSearchAct")
						.finish();
			}
		} else if (this instanceof CollectionAct) {
			if (ValueStack.getInstance().activitys.containsKey("CollectionAct")) {
				ValueStack.getInstance().activitys.get("CollectionAct")
						.finish();
			}
		} else if (this instanceof CollectionGridAct) {
			if (ValueStack.getInstance().activitys
					.containsKey("CollectionGridAct")) {
				ValueStack.getInstance().activitys.get("CollectionGridAct")
						.finish();
			}
		}

		// else if (this instanceof DetaiAct) {
		// if (ValueStack.getInstance().activitys.containsKey("DetaiAct")) {
		// ValueStack.getInstance().activitys.get("DetaiAct").finish();
		// }
		// }

		else if (this instanceof EidtPicAct) {
			if (ValueStack.getInstance().activitys.containsKey("EidtPicAct")) {
				ValueStack.getInstance().activitys.get("EidtPicAct").finish();
			}
		}

		else if (this instanceof MainAct) {
			if (ValueStack.getInstance().activitys.containsKey("MainAct")) {
				ValueStack.getInstance().activitys.get("MainAct").finish();
			}
		}

		else if (this instanceof NameSearchAct) {
			if (ValueStack.getInstance().activitys.containsKey("NameSearchAct")) {
				ValueStack.getInstance().activitys.get("NameSearchAct")
						.finish();
			}
		}

		else if (this instanceof PropertyAct) {
			if (ValueStack.getInstance().activitys.containsKey("PropertyAct")) {
				ValueStack.getInstance().activitys.get("PropertyAct").finish();
			}
		}

		else if (this instanceof SartAct) {
			if (ValueStack.getInstance().activitys.containsKey("SartAct")) {
				ValueStack.getInstance().activitys.get("SartAct").finish();
			}
		}

		else if (this instanceof SettingAct) {
			if (ValueStack.getInstance().activitys.containsKey("SettingAct")) {
				ValueStack.getInstance().activitys.get("SettingAct").finish();
			}
		}

		else if (this instanceof TypeAct) {
			if (ValueStack.getInstance().activitys.containsKey("TypeAct")) {
				ValueStack.getInstance().activitys.get("TypeAct").finish();
			}
		}

		else if (this instanceof UnunloadAct) {
			if (ValueStack.getInstance().activitys.containsKey("UnunloadAct")) {
				ValueStack.getInstance().activitys.get("UnunloadAct").finish();
			}
		}

		else if (this instanceof UnunloadAnimalAct) {
			if (ValueStack.getInstance().activitys
					.containsKey("UnunloadAnimalAct")) {
				ValueStack.getInstance().activitys.get("UnunloadAnimalAct")
						.finish();
			}
		}

		else if (this instanceof UnunloadBirdAct) {
			if (ValueStack.getInstance().activitys
					.containsKey("UnunloadBirdAct")) {
				ValueStack.getInstance().activitys.get("UnunloadBirdAct")
						.finish();
			}
		}

		else if (this instanceof UserLoginAct) {
			if (ValueStack.getInstance().activitys.containsKey("UserLoginAct")) {
				ValueStack.getInstance().activitys.get("UserLoginAct").finish();
			}
		} else if (this instanceof UserLoginAct) {
			if (ValueStack.getInstance().activitys.containsKey("SettingAct")) {
				ValueStack.getInstance().activitys.get("SettingAct").finish();
			}
		}

	}

	protected void messageUser() {
		AlertDialog.Builder builder = new Builder(this);
		builder.setMessage("退出当前终端?");
		builder.setTitle("友情提示");
		builder.setPositiveButton("确定",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						ValueStack.getInstance().exit();
						System.exit(0);
						AppConstants.shutdown();
						dialog.dismiss();
					}
				});
		builder.setNeutralButton("取消",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}

	// ----------弹出框S
	View popSearchView;
	Dialog search_dialgo;

	private ImageView icon_menu1 = null;
	private ImageView icon_menu2 = null;
	private ImageView icon_menu3 = null;
	private ImageView icon_menu4 = null;
	private LinearLayout.LayoutParams mParams = null;

	protected void initPopupForMenu() {
		if (popSearchView == null) {
			popSearchView = this.getLayoutInflater().inflate(
					R.layout.pop_menu_window, null);
			mParams = new LinearLayout.LayoutParams(260, 220);
			mParams.gravity = Gravity.CENTER;
			search_dialgo = new Dialog(this, R.style.customer);
			search_dialgo.setCanceledOnTouchOutside(false);
			search_dialgo.requestWindowFeature(Window.FEATURE_NO_TITLE);
			search_dialgo.setContentView(popSearchView, mParams);
			menu1 = (ImageView) popSearchView.findViewById(R.id.icon_menu1);
			icon_menu2 = (ImageView) popSearchView
					.findViewById(R.id.icon_menu2);
			icon_menu3 = (ImageView) popSearchView
					.findViewById(R.id.icon_menu3);
			icon_menu4 = (ImageView) popSearchView
					.findViewById(R.id.icon_menu4);
			menu1.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					search_dialgo.dismiss();
					DetaiAct.MODE = DetaiAct.ADDMODE;
					Intent intent = new Intent(BaseAct.this, DetaiAct.class);
					startActivity(intent);
				}
			});
			icon_menu2.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					search_dialgo.dismiss();
					Intent intent = new Intent(BaseAct.this, TypeAct.class);
					startActivity(intent);
				}
			});
			icon_menu3.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					search_dialgo.dismiss();
					Intent intent = new Intent(BaseAct.this, SettingAct.class);
					startActivity(intent);
				}
			});
			icon_menu4.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					search_dialgo.dismiss();
					messageUser();
				}
			});
		}

		search_dialgo.show();
	}

	private ImageView menu1 = null;
	private ImageView menu2 = null;

	protected void initBottomBar() {
		menu1 = (ImageView) findViewById(R.id.menu1);
		menu2 = (ImageView) findViewById(R.id.menu2);
		menu1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				initPopupForMenu();
			}
		});
		menu2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	// ----------弹出框S
	View popSearchView2;
	Dialog search_dialgo2;

	private TextView mline1 = null;
	private TextView mline2 = null;
	private TextView title = null;

	private LinearLayout.LayoutParams mParams2 = null;
	private DataService mDataService = null;

	protected void initPopupMenu(final BirdMessage birdMessage) {
		if (popSearchView == null) {
			mDataService = new DataService(BaseAct.this);
			popSearchView = this.getLayoutInflater().inflate(
					R.layout.pop_window, null);
			mParams2 = new LinearLayout.LayoutParams(200, 150);
			mParams2.gravity = Gravity.CENTER;
			search_dialgo = new Dialog(this, R.style.customer);
			search_dialgo.setCanceledOnTouchOutside(false);
			search_dialgo.requestWindowFeature(Window.FEATURE_NO_TITLE);
			search_dialgo.setContentView(popSearchView, mParams2);
			mline1 = (TextView) popSearchView.findViewById(R.id.mline1);
			title = (TextView) popSearchView.findViewById(R.id.title);
			mline1.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					search_dialgo.dismiss();
					ValueStack.getInstance().setValue("bean", birdMessage);
					DetaiAct.MODE = DetaiAct.EDITMODE;
					Intent intent = new Intent(BaseAct.this, DetaiAct.class);
					startActivity(intent);
				}
			});

			mline2 = (TextView) popSearchView.findViewById(R.id.mline2);
			mline2.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					ValueStack.getInstance().setValue("bean", birdMessage);
					Intent intent = new Intent(BaseAct.this, PropertyAct.class);
					startActivity(intent);
					search_dialgo.dismiss();

				}
			});
		}

		mDataService.updateBirdMessageClick(birdMessage);
		title.setText(birdMessage.birdName);
		search_dialgo.show();
	}

	// ----------弹出框E
}
